package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Assistant;
import com.gxuwz.zlc.entity.Lab;
import com.gxuwz.zlc.entity.Student;
import com.gxuwz.zlc.entity.User;
import com.gxuwz.zlc.service.ApplyService;
import com.gxuwz.zlc.service.StudentService;
import com.gxuwz.zlc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生
 */
@Controller
@RequestMapping("/student2")
public class StudentController {
    // 学生
    @Autowired
    private StudentService studentService;
    // 用户
    @Autowired
    private UserService userService;
    // 报名表
    @Autowired
    private ApplyService applyService;

//    // 查询所有
//    @RequestMapping("/findAll")
//    @ResponseBody
//    public List<Student> findStudent(){
//        List<Lab> labList = labService.findLab();
//        return labList;
//    }
//

    /**
     * 根据学号查询一个学生
     * @param stuId
     * @return
     */
    @GetMapping("/findOne/{stuId}")
    @ResponseBody
    public Result get(@PathVariable String stuId){
        Student studentById = studentService.findStudentById(stuId);
        // code=0,msg=成功
        return Result.success(studentById);
    }


    /**
     * 学生注册
     * 添加学生信息
     * @param student
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map addStudent(@RequestBody Student student){
        Map<String,String> map = new HashMap<>();
        try {
            // 将信息存入学生表
            studentService.addStudent(student);
            // 将信息存入user用户表
            User user = new User();
            user.setUsername(student.getStuId());
            user.setPassword(student.getPassword());
            user.setPerms("student");
            userService.addUser(user);
            map.put("code","200");
            map.put("msg","注册成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","注册失败,该学号已被注册");
            e.printStackTrace();
        }
        return map;
    }

}
